package com.sun.xacml.attr;

import java.net.URI;
import java.util.List;

import org.jdom.Namespace;
import org.jdom.input.DOMBuilder;
import org.w3c.dom.Element;

import com.vividsolutions.jts.geom.LineString;
import com.vividsolutions.jts.geom.LinearRing;
import com.vividsolutions.jts.io.ParseException;
import com.vividsolutions.jts.io.WKTReader;

public class LinearRingAttribute extends AttributeValue {
	private static URI identifierURI;
	private static String srsName;
	private static LinearRing geometry;

	/**
	 * Constructor tao LinearRing attribute tu jdomRoot(duoi dang gml encode)
	 * 
	 */
	public LinearRingAttribute(org.jdom.Element jdomRoot) {
		super(identifierURI);

		Namespace gml = null;
		String str = "";//chuoi toa do 
		
		List additionalNamespaces = jdomRoot.getAdditionalNamespaces();
		for (int ix = 0; ix < additionalNamespaces.size(); ix++)
			if ("gml".equals(((Namespace) additionalNamespaces.get(ix))
					.getPrefix()))
				gml = (Namespace) additionalNamespaces.get(ix);

		//lay srsName
		srsName = jdomRoot.getAttributeValue("srsName");

		//qua trinh lay toa do cua LinearRing
		org.jdom.Element coord = jdomRoot.getChild("coord", gml);
		if (coord != null) {
			List<org.jdom.Element> coordList = jdomRoot.getChildren("coord",gml);
			for (int i = 0; i < coordList.size(); i++) {
				String X = "";
				String Y = "";
				X = coordList.get(i).getChild("X", gml).getValue();
				Y = coordList.get(i).getChild("Y", gml).getValue();
				str = str + X + " " + Y;
				if (i != coordList.size() - 1)
					str = str + ",";
			}
		} else 	str = jdomRoot.getChild("coordinates", gml).getValue();
			
		try {
			geometry = (LinearRing) new WKTReader().read("LINEARRING(" + str + ")");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Constructor tao LinearRing attribute tu DomRoot
	 * 
	 */
	public LinearRingAttribute(Element domRoot) {
		super(identifierURI);

		//chuyen tu DOM sang jdom
		DOMBuilder domBuilder = new DOMBuilder();
		org.jdom.Element jdomRoot = domBuilder.build(domRoot);
		
		Namespace gml = null;
		String str = "";
		List additionalNamespaces = jdomRoot.getAdditionalNamespaces();
		for (int ix = 0; ix < additionalNamespaces.size(); ix++)
			if ("gml".equals(((Namespace) additionalNamespaces.get(ix)).getPrefix()))
				gml = (Namespace) additionalNamespaces.get(ix);

		//lay srsName
		srsName = jdomRoot.getAttributeValue("srsName");

		//qua trinh lay toa do cua LinearRing
		org.jdom.Element coord = jdomRoot.getChild("coord", gml);
		if (coord != null) {
			List<org.jdom.Element> coordList = jdomRoot.getChildren("coord", gml);
			for (int i = 0; i < coordList.size(); i++) {
				String X = "";
				String Y = "";
				X = coordList.get(i).getChild("X", gml).getValue();
				Y = coordList.get(i).getChild("Y", gml).getValue();
				str = str + X + " " + Y;
				if (i != coordList.size() - 1) str = str + ",";
			}
		} else 	str = jdomRoot.getChild("coordinates", gml).getValue();		
		
		//chuyen sang gia tri jts
		try {
			geometry = (LinearRing) new WKTReader().read("LINEARRING(" + str + ")");			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	//get geometry
	public LinearRing getGeometry() {
		return geometry;
	}

	//get srsName
	public String getSrsName() {
		return srsName;
	}

	@Override
	public String encode() {
		// TODO Auto-generated method stub
		return geometry.toString();
	}

	public static AttributeValue getInstance(org.jdom.Element root)
			throws ParseException {
		// TODO Auto-generated method stub
		return getInstance((Element) root);
	}

	public static AttributeValue getInstance(Element root)
			throws ParseException {
		// TODO Auto-generated method stub
		return new LinearRingAttribute(root);
	}
}
